package com.hxkcloud.patient.activitys;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.MonitorEditAdapter;
import com.hxkcloud.patient.beans.MonitorInputEditBean;
import com.hxkcloud.patient.beans.MonitorKindBean;
import com.hxkcloud.patient.dborm.MonitorKindDao;
import com.hxkcloud.patient.popup.DateTimeSelectPopWindow;
import com.hxkcloud.patient.popup.MonitorInputPopWindow;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-08-04.
 * Description:患者监测项编辑
 */
public class MonitorEditActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private TextView tv_time;
    private LinearLayout ll_kind;
    private TextView tv_test;
    private RecyclerView rv;
    private Button bt_save;

    private DateTimeSelectPopWindow datetimeSelect;
    private MonitorInputPopWindow monitorInputPopWindow;

    private String monitorId;
    private MonitorKindBean kind;
    private MonitorEditAdapter adapter;

    private String testId;//选择的餐前或餐后

    private String pointTime;
    private String pointId;
    private String pointId1;
    private String pointId2;
    private String value;
    private String value1;
    private String value2;

    private boolean isValueInput = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitorinput);
        if (getIntent().hasExtra("monitorId")) {
            monitorId = this.getIntent().getStringExtra("monitorId");
        }
        if (getIntent().hasExtra("time")) {
            pointTime = this.getIntent().getStringExtra("time");
        }
        if (getIntent().hasExtra("pointId")) {
            pointId = this.getIntent().getStringExtra("pointId");
        }
        if (getIntent().hasExtra("pointId1")) {
            pointId1 = this.getIntent().getStringExtra("pointId1");
        } else {
            pointId1 = "";
        }
        if (getIntent().hasExtra("pointId2")) {
            pointId2 = this.getIntent().getStringExtra("pointId2");
        } else {
            pointId2 = "";
        }
        if (getIntent().hasExtra("value")) {
            value = this.getIntent().getStringExtra("value");
        } else {
            value = "";
        }
        if (getIntent().hasExtra("value1")) {
            value1 = this.getIntent().getStringExtra("value1");
        } else {
            value1 = "";
        }
        if (getIntent().hasExtra("value2")) {
            value2 = this.getIntent().getStringExtra("value2");
        } else {
            value2 = "";
        }
        MonitorKindDao monitorKindDao = new MonitorKindDao(mContext);
        kind = monitorKindDao.getKind(monitorId);
        if (kind == null) {
            toastMsg(R.string.cantfindmonitordata);
            finish();
            return;
        }
        initView();
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_PATIENTMONITOREDIT_SUCCESS:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    setResult(RESULT_OK);
                    finish();
                    return;
                } else {
                    toastMsg(getResources().getString(R.string.datainputsuccess));
                }
                break;
            case MessageCode.NETWORKCODE_PATIENTMONITOREDIT_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                } else {
                    toastMsg(getResources().getString(R.string.datainputfail));
                }
                break;
        }
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(kind.getName() + "修改");
        iv_back.setOnClickListener(this);

        tv_time = (TextView) findViewById(R.id.tv_monitorinput_time);
        if (pointTime != null) {
            long timeStamp = Long.parseLong(pointTime);
            String pointTime = Utils.timeStamp2Date((timeStamp) + "", "yyyy-MM-dd HH:mm:ss");
            tv_time.setText(pointTime);
        }

        ll_kind = (LinearLayout) findViewById(R.id.ll_monitorinput_kind);
        tv_test = (TextView) findViewById(R.id.tv_monitorinput_kind);
        rv = (RecyclerView) findViewById(R.id.rv_monitorinput);
        bt_save = (Button) findViewById(R.id.bt_monitorinput_save);

        tv_time.setOnClickListener(this);
        if (kind.getTestList() != null && kind.getTestList().size() > 0) {
            ll_kind.setVisibility(View.VISIBLE);
            tv_test.setText(kind.getTestList().get(0).getName());
            testId = kind.getTestList().get(0).getTestId();
            tv_test.setOnClickListener(this);
        } else {
            ll_kind.setVisibility(View.GONE);
        }

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(mLayoutManager);
        adapter = new MonitorEditAdapter(mContext, kind, value, value1, value2);
        adapter.setListener(new MonitorEditAdapter.onWordsChangeListener() {
            @Override
            public void showGreen() {
                isValueInput = true;
                bt_save.setBackgroundResource(R.drawable.bt_green);
                bt_save.setEnabled(true);

            }

            @Override
            public void showGrey() {
                isValueInput = false;
                bt_save.setBackgroundResource(R.drawable.bt_grey);
                bt_save.setEnabled(false);
            }
        });
        rv.setAdapter(adapter);

        bt_save.setOnClickListener(this);
        bt_save.setBackgroundResource(R.drawable.bt_green);
        bt_save.setEnabled(true);

        datetimeSelect = new DateTimeSelectPopWindow(this);
        datetimeSelect.setTimeListener(new DateTimeSelectPopWindow.TimeListener() {
            @Override
            public void setTime(String time) {
                String timeStr = "";
                if (time.contains(".")) {
                    timeStr = time.replace(".", "-");
                }
                tv_time.setText(timeStr);
            }
        });
        monitorInputPopWindow = new MonitorInputPopWindow(this, kind.getTestList());
        monitorInputPopWindow.setListener(new MonitorInputPopWindow.onTestSelectListener() {
            @Override
            public void select(int position) {
                tv_test.setText(kind.getTestList().get(position).getName());
                testId = kind.getTestList().get(position).getTestId();
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_monitorinput_time:
                datetimeSelect.show(ll_title);
                break;
            case R.id.tv_monitorinput_kind:
                monitorInputPopWindow.show(ll_title);
                break;
            case R.id.bt_monitorinput_save:
                String time = tv_time.getText().toString().trim();
                if (TextUtils.isEmpty(time)) {
                    toastMsg(R.string.pleaseinputwholecontent);
                    return;
                }
                String[] vs = adapter.getValues();
                List<MonitorInputEditBean> values = new ArrayList<>();
                for (int i = 0; i < vs.length; i++) {
                    if (TextUtils.isEmpty(vs[i])) {
                        toastMsg(R.string.pleaseinputwholecontent);
                        return;
                    }
                    double max = Double.valueOf(kind.getMaxValue());
                    double min = Double.valueOf(kind.getMinValue());
                    double input = Double.valueOf(vs[i]);
                    if (input < min || input > max) {
                        toastMsg(R.string.inputoutofperiod);
                    }
                    MonitorInputEditBean value = new MonitorInputEditBean();
                    value.setValue(vs[i]);
//                    value.setValueId(kind.getValueList().get(i).getValueId());
                    if (i == 0) {
                        value.setPointId(pointId);
                    } else if (i == 1) {
                        value.setPointId(pointId1);
                    } else if (i == 2) {
                        value.setPointId(pointId2);
                    }
                    values.add(value);
                }
                loading();
                long timeStamp = Long.parseLong(Utils.date2TimeStamp(time + ":00", "yyyy-MM-dd HH:mm:ss"));
                YiJiaHuNetWorkUtils.patientMonitorEdit(loginInfo.getUserId(), timeStamp + "", monitorId, testId, values, handler);
                break;
        }
    }
}
